package com.framework.mybatis.plugin;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.util.StringUtility;

import java.util.List;
import java.util.Properties;

/**
 * 生成实体集成BaseEntity
 *
 * @author LeeJun
 * @create 2017-08-12 15:37
 */
public class SuperEntityPlugin extends PluginAdapter {

	private String superClass;
	private String superClassPackage;

	@Override
	public void setProperties(Properties properties) {
		super.setProperties(properties);
		String superClass = this.properties.getProperty("superClass");
		if (StringUtility.stringHasValue(superClass)) {
			this.superClass = superClass;
			this.superClassPackage = superClass.substring(superClass.lastIndexOf(".") + 1);
		} else {
			throw new RuntimeException("superClass 属性不能为空！");
		}
	}

	@Override
	public boolean validate(List<String> warnings) {
		return true;
	}

	@Override
	public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
												 IntrospectedTable introspectedTable) {

		topLevelClass.setSuperClass(superClass);
		if (!topLevelClass.getType().getPackageName().equals(this.superClassPackage)) {
			topLevelClass.addImportedType(superClass);
		}
		return true;
	}
}
